package com.whq.knowverywell.task.update;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import android.net.Uri;
import android.os.Environment;

public class DownloadResumeInfo implements Serializable {
	private static final long serialVersionUID = 5327862921231122377L;
	private String m_fileName;
	private String m_version;
	private int m_fileSize;
	private int m_downloadedSize;
	private long m_gernerateTime;

	public DownloadResumeInfo() {
		m_gernerateTime = System.currentTimeMillis();
	}

	public void setFileName(String fileName) {
		m_fileName = fileName;
	}

	public String getFileName() {
		return m_fileName;
	}

	public void setVersion(String version) {
		m_version = version;
	}

	public String getVersion() {
		return m_version;
	}

	public void setFileSize(int fileSize) {
		m_fileSize = fileSize;
	}

	public int getFileSize() {
		return m_fileSize;
	}

	public long getGenerateTime() {
		return m_gernerateTime;
	}

	public void setDownloadedSize(int downloadedSize) {
		m_downloadedSize = downloadedSize;
	}

	public int getDownloadedSize() {
		return m_downloadedSize;
	}

	public boolean saveResumeInfo() {
		boolean bSaveOk = false;
		String fileInfo = Environment.getExternalStorageDirectory() + "/know-very-well_" + m_version + ".info";
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(fileInfo);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(this);
			oos.close();
			bSaveOk = true;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bSaveOk;

	}

	public Uri Finish() {
		String tmpPath = Environment.getExternalStorageDirectory() + "/know-very-well_" + m_version + ".tmp";

		String apkPath = Environment.getExternalStorageDirectory() + "/know-very-well_" + m_version + ".apk";

		String fileInfo = Environment.getExternalStorageDirectory() + "/know-very-well_" + m_version + ".info";

		File tmpFile = new File(tmpPath);
		File apkFile = new File(apkPath);
		File infoFile = new File(fileInfo);

		infoFile.delete();

		tmpFile.renameTo(apkFile);

		return Uri.fromFile(apkFile);
	}
}
